<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>登陆页面</title>
<link rel="stylesheet" href="./css/bootstrap.min.css">
<style>
.form-horizontal div.form-group{
    margin-left:0;
    margin-right:15px;
}
</style>
</head>
<body>
<div class="h1 text-center">登陆页面</div>
<form class="form-horizontal">
    <div class="form-group">
        <label for="inputEmail3" class="col-sm-2 control-label">用户名：</label>
        <div class="col-sm-10">
        <input type="text" class="form-control" id="username" placeholder="请输入用户名">
        </div>
    </div>
    <div class="form-group">
        <label for="inputPassword3" class="col-sm-2 control-label">密码：</label>
        <div class="col-sm-10">
        <input type="password" class="form-control" id="password" placeholder="请输入密码">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
        <div class="checkbox">
            <label>
            <input type="checkbox" name="remember"> 记住
            </label>
        </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
        <button type="button" class="btn btn-default">登陆</button>
        </div>
    </div>
</form>
</body>
<script src="./js/promiseAjax.js"></script>
<script src="./js/cookie.js"></script>
<script type="text/javascript">
var u = document.querySelector("#username");
var uname = getCookie("uname");
// console.log(uname);
if(uname){
    u.value = uname;
}else{
    u.value = '';
}

var btn = document.querySelector("button[type='button']");
btn.addEventListener("click",login);
function login(){
    var username = u.value.trim();
    var password = document.querySelector("#password").value.trim();

    var rem = document.querySelector("[name='remember']");
    pAjax({
        url:"./server/login.php",
        type:"post",
        data:{
            username:username,
            password:password
        }
    }).then(function(res){
        // console.log(res);
        if(res.status==200){
            setCookie("username",username,7200);
            if(rem.checked){
                setCookie("uname",username,3600*24*7);
            }else{
                removeCookie("uname");
            }
            alert(res.msg);
            location.href = 'home.html';
        }else{
            alert(res.msg);
            return;
        }
    });
}
</script>
</html>